/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.repository.common;


import com.inspur.edp.cef.entity.config.CefConfig;
import com.inspur.edp.cef.entity.config.CefExtendConfig;
import com.inspur.edp.cef.gencommon.config.CefConfigUtils;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

public abstract class CefPreCompileManager {

    private Class<?> cls = null;

    protected abstract String getConfigTypeName();

    public void build(CompileContext context){
        loadConfig(context);
        execute(context);
    }

    protected abstract void execute(CompileContext context);

    private void loadConfig(CompileContext context) {
        String className = getConfigTypeName();
        String path = context.getCoreJarFileName();
        ArrayList<String> jars = new ArrayList<String>(){
            {
                add(path);
            }
        };
        cls = CefConfigUtils.getType(jars, null, className);
//        File file = new File(path);
//        if (!file.exists()) {
//            throw new RuntimeException("没有找到Jar包"+path);
//        }
//        try {
//            URL url = file.toURI().toURL();
//            System.out.println(url.toString());
//
//            URLClassLoader classLoader = new URLClassLoader(new URL[]{url},
//                    Thread.currentThread().getContextClassLoader());
//            cls = classLoader.loadClass(className);
//
//        } catch (ClassNotFoundException e) {
//            throw new RuntimeException("没有找到类"+className);
//        } catch (MalformedURLException e) {
//            throw new RuntimeException("Jar包"+path+"解析失败");
//        }
    }

    protected ArrayList<CefConfig> getConfigs(){
        Method method = null;
        try {
            method = cls.getDeclaredMethod("getConfigs");
            if (!method.isAccessible()) {
                method.setAccessible(true);
            }
            return (ArrayList<CefConfig>)method.invoke(cls.newInstance());

        } catch (NoSuchMethodException e) {
            throw new RuntimeException("找不到方法getConfigs",e);
        } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
            throw new RuntimeException("转调方法getConfigs失败",e);
        }
    }

    protected ArrayList<CefExtendConfig> getExtendConfigs() {
        Method method = null;
        try {
            method = cls.getDeclaredMethod("getExtendConfigs");
            if (!method.isAccessible()) {
                method.setAccessible(true);
            }
            return (ArrayList<CefExtendConfig>)method.invoke(cls.newInstance());

        } catch (NoSuchMethodException e) {
            throw new RuntimeException("找不到方法getConfigs",e);
        } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
            throw new RuntimeException("转调方法getConfigs失败",e);
        }
    }
}
